package com.gamecauca.item;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.gamecauca.Assets;
import com.gamecauca.World;
import com.gamecauca.common.Constant;
import com.gamecauca.common.GameObject;
import com.gamecauca.item.GameItem;

public class Bomb extends GameItem {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7834351313448497379L;
	private int x_0;// toa do x ban dau
	private int y_0;// toa do y ban dau
	
	private boolean isFired;
	private int iFrameFire;
	private int iFrameThrow;
	private boolean bombFailed;
	private boolean finished;
	
	private int fireCenterWidth;
	private int fireCenterHeight;
	
//	private int xBombNext;
	private float speedBomb;
	private int id;
	private int index;
	private int countTime;
	private int bombTime;
	private float tempX;
	private float tempY;
	private int kind;
	
//	public Bomb(){
//		super(Constant.BOMBANG);
//		kindOfObject = Constant.TOOLS;
//	}
	public Bomb(int kind){
		super(kind);
		kindOfObject = Constant.TOOLS;
	}
	public Bomb(int kind,int x,int y,float x_speed, float speedBoomb, int direct, int index, int id){
		super(kind,Constant.ITEMINFISHING,x, y, 64, 64);
		this.kind = kind;
		position.set(x, y);
		x_Speed = x_speed;
		this.speedBomb = speedBoomb;
		x_0 = x;
		y_0 = y;
		direction = direct;
		isFired = false;
		bombFailed = false;
		iFrameFire = 0;
		iFrameThrow = 0;
		iTimer = 0;
		
		this.id = id;
		this.index = index;
		
		bombTime = 3;

	}
	public void update(){
		//System.out.println("boom update ne :====================================");
		
		if(this.position.y < 0 || this.position.x < 0 || this.position.x > Constant.SCREEN_WIDTH)
		{
			this.setBombFailed(true);
		}
		
		if(isFired)
		{
			
			iTimer++;
			if(iTimer>1000000) iTimer = 0;
			if(iTimer%15 == 0)iFrameFire++;
			//if(iFrame > 3)iFrame = 0;
			
			x_Speed +=0f;
		}
		else
		{
			iTimer++;
			if(iTimer>1000000) iTimer = 0;
			if(iTimer%10 == 0)iFrameThrow++;
			if(iFrameThrow > 5)iFrameThrow = 0;
			
			x_Speed += 0.2f;
		}
			
		if(direction == Constant.LEFT){
		
			
			tempY = y_0 - (x_Speed * x_Speed);
			
			if(tempY <= 2){
				tempY = 2;
			}
			else{
				tempX = x_0 - (x_Speed * speedBomb);
			}
			
			position.set(tempX, tempY);
		}
		else if(direction == Constant.RIGHT){

			
			tempY = y_0 - (x_Speed * x_Speed);
			
			if(tempY <= 2){
				tempY = 2;
			}
			else{
				tempX = x_0 + (x_Speed * speedBomb);
			}
			
			position.set(tempX, tempY);
		}
		
		
	}
	public void render(SpriteBatch batch){
//		System.out.println("kind of Cloud :===================================="+kind);
		if(isFired)
		{
			if(iFrameFire > 5)
				this.setFinished(true);
			else{
				
				this.setFireCenterWidth(Assets.fireBombRegionWidth[this.getIndex() - 1]);
				this.setFireCenterHeight(Assets.fireBombRegionHeight[this.getIndex() - 1]);
				

				batch.draw(Assets.fireBombTextureRegion[this.getIndex() - 1][iFrameFire], 
					position.x - (this.getFireCenterWidth()/2), position.y - (this.getFireCenterHeight()/2));
			}
		}
		else
		{	
			
//			Assets.bomb = Assets.itemImgs[this.getId()];
			if(direction == Constant.LEFT){
				
				batch.draw(Assets.leftBombRegion[this.getIndex() - 1][iFrameThrow], position.x, position.y);
			}
			else if(direction == Constant.RIGHT){
				
				batch.draw(Assets.rightBombRegion[this.getIndex() - 1][iFrameThrow], position.x, position.y);
			}
			
		
			if(this.getIndex() >= 3){
				
				countTime++;
				if(bombTime > 0){ 
					if(countTime % 24 == 0) bombTime--;
					Assets.font.draw(batch,String.valueOf(bombTime),position.x + 32, position.y + 64);
				}
				else{
					
					Assets.font.draw(batch,String.valueOf(bombTime),position.x + 32, position.y + 64);
					bombTime--;
				}
				
				
			}
		}
			

	}
	public void setFired(boolean isFired) {
		this.isFired = isFired;
	}
	public boolean isFired() {
		return isFired;
	}
	public void setBombFailed(boolean bombFailed) {
		this.bombFailed = bombFailed;
	}
	public boolean isBombFailed() {
		return bombFailed;
	}
	public void setFinished(boolean finished) {
		this.finished = finished;
	}
	public boolean isFinished() {
		return finished;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getIndex() {
		return index;
	}
	public void setCountTime(int countTime) {
		this.countTime = countTime;
	}
	public int getCountTime() {
		return countTime;
	}
	public void setBombTime(int bombTime) {
		this.bombTime = bombTime;
	}
	public int getBombTime() {
		return bombTime;
	}
	public void setSpeedBomb(float speedBomb) {
		this.speedBomb = speedBomb;
	}
	public float getSpeedBomb() {
		return speedBomb;
	}
	public void setTempX(float tempX) {
		this.tempX = tempX;
	}
	public float getTempX() {
		return tempX;
	}
	public void setTempY(float tempY) {
		this.tempY = tempY;
	}
	public float getTempY() {
		return tempY;
	}
	public void setFireCenterWidth(int fireCenterWidth) {
		this.fireCenterWidth = fireCenterWidth;
	}
	public int getFireCenterWidth() {
		return fireCenterWidth;
	}
	public void setFireCenterHeight(int fireCenterHeight) {
		this.fireCenterHeight = fireCenterHeight;
	}
	public int getFireCenterHeight() {
		return fireCenterHeight;
	}
	public void setKind(int ikind) {
		kind = ikind;
	}
	public int getKind() {
		return kind ;
	}
	
}
